package com.tencent.iov.cowin.befsale.clue.controller.clue.assembler;

import com.google.common.collect.Lists;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.request.UpdateClueIntentionRequest;
import com.tencent.iov.cowin.befsale.clue.dao.http.tenant.response.DictionariesResponse;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueIntention;
import com.software.boot.common.model.dao.BaseAssembler;
import com.software.boot.common.model.rest.BaseRequest;
import com.software.boot.common.model.rest.BaseResponse;
import com.software.boot.common.util.EmptyUtils;
import com.software.common.config.SearchLocalHeader;
import com.software.common.enums.SCRMRequestHeaderEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: whp
 * @Date: 2021-12-21 16:16:16
 * @Description: 线索意向信息适配器，负责vo<->entity、vo<->po的转换和必要的参数检查，行为
 */
@Slf4j
public class ClueIntentionAssembler extends BaseAssembler<BaseResponse, BaseRequest, ClueIntention> {

    public static ClueIntention initUpdateClueIntentionRequest(ClueIntention request){
        request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        request.setUpdatedTime(new Date());
        return request;
    }

    public static void initUpdateByClueIntention(UpdateClueIntentionRequest request, ClueIntention clueIntention){
        request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        if (request.getBuyWay() != null) {
            clueIntention.setBuyWay(request.getBuyWay());
        }
        if (request.getMayBuyTime() != null) {
            clueIntention.setMayBuyTime(request.getMayBuyTime());
        }
        if (request.getBuyVehicleType() != null) {
            clueIntention.setBuyVehicleType(request.getBuyVehicleType());
        }
        if (request.getBuyVehicleUse() != null) {
            clueIntention.setBuyVehicleUse(request.getBuyVehicleUse());
        }
        if (request.getBuyVehiclePrice() != null) {
            clueIntention.setBuyVehiclePrice(request.getBuyVehiclePrice());
        }
    }

    /**
     * 获取意向基本信息的拼接
     * @param dictionariesResponses 字典信息
     * @param clueIntention 意向信息
     * @return 意向信息拼接
     */
    public static String getIntentionTags(List<DictionariesResponse> dictionariesResponses, ClueIntention clueIntention){
        List<String> intentionCodeList = Lists.newArrayList();
        dictionariesResponses.forEach(dictionariesResponse -> {
            if (EmptyUtils.isNotEmpty(clueIntention.getBuyWay()) && clueIntention.getBuyWay().contains(dictionariesResponse.getCode())) {
                intentionCodeList.add(dictionariesResponse.getName());
            }
            if (EmptyUtils.isNotEmpty(clueIntention.getMayBuyTime()) && clueIntention.getMayBuyTime().contains(dictionariesResponse.getCode())) {
                intentionCodeList.add(dictionariesResponse.getName());
            }
            if (EmptyUtils.isNotEmpty(clueIntention.getBuyVehicleType()) && clueIntention.getBuyVehicleType().contains(dictionariesResponse.getCode())) {
                intentionCodeList.add(dictionariesResponse.getName());
            }
            if (EmptyUtils.isNotEmpty(clueIntention.getBuyVehicleUse()) && clueIntention.getBuyVehicleUse().contains(dictionariesResponse.getCode())) {
                intentionCodeList.add(dictionariesResponse.getName());
            }
            if (EmptyUtils.isNotEmpty(clueIntention.getBuyVehiclePrice()) && clueIntention.getBuyVehiclePrice().contains(dictionariesResponse.getCode())) {
                intentionCodeList.add(dictionariesResponse.getName());
            }
        });
        return StringUtils.join(intentionCodeList, ",");
    }
}
